﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;
using DataEntity;
using Model;

namespace SWA_View
{
    public partial class Zihang : Form
    {
        int _ProjectID;
        public ObservableCollection<ChiShuiEntity> ChiShuiEntitys = null;
        public ObservableCollection<SpeedEntity> SpeedEntitys = null;
        private ObservableCollection<int> DelChiShuiEntitys = new ObservableCollection<int>();
        private ObservableCollection<int> DelSpeedEntitys = new ObservableCollection<int>();
        public ObservableCollection<ExperimentCombinationEntity> result = new ObservableCollection<ExperimentCombinationEntity>();
        public Zihang(int ProjectID)
        {
            InitializeComponent();
            _ProjectID = ProjectID;
            CommonModel model = new CommonModel();
            ChiShuiEntitys = model.Query<ChiShuiEntity>("ProjectID=" + ProjectID + " and ExperimentType='" + ExperimentType.自航实验 + "'");
            SpeedEntitys = model.Query<SpeedEntity>("ProjectID=" + ProjectID + " and ExperimentType='" + ExperimentType.自航实验 + "'");
            ChiShuiEntitys.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ChiShuiEntitys_CollectionChanged);
            SpeedEntitys.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(SpeedEntitys_CollectionChanged);
            SpeedEntitys_CollectionChanged(null, null);
            ChiShuiEntitys_CollectionChanged(null, null);
            DelChiShuiEntitys.Clear();
            DelSpeedEntitys.Clear();
        }

        void SpeedEntitys_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            listViewSpeed.Items.Clear();
            ListViewItem item = null;
            foreach (var c in SpeedEntitys)
            {
                item = new ListViewItem(c.Speed + "");
                item.Tag = c;               
                listViewSpeed.Items.Add(item);
            }
        }

        void ChiShuiEntitys_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            listViewDraft.Items.Clear();
            ListViewItem item = null;
            foreach (var c in ChiShuiEntitys)
            {
                item = new ListViewItem(c.ShouChiShui+"");
                item.Tag = c;
                item.SubItems.Add(c.WeiChiShui + "");
                item.SubItems.Add(c.DSC);
                listViewDraft.Items.Add(item);
            }
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {

            DialogResult rsltEnsure = MessageBox.Show("确定要退出吗？", "确认关闭", MessageBoxButtons.OKCancel);
            if (rsltEnsure == DialogResult.OK)
            {

                this.DialogResult = DialogResult.Cancel;
               
            }
            else
            {

            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (ChiShuiEntitys.Count <= 0 || SpeedEntitys.Count <= 0)
            {
                MessageBox.Show("请填写实验数据");
                return;
            }
            result.Clear();
            CommonModel model = new CommonModel();
            if (DelChiShuiEntitys.Count > 0)
            {
                string Conditin = "ID in (" + DelChiShuiEntitys[0];
                for (int i = 1; i < DelChiShuiEntitys.Count; i++)
                {
                    Conditin += " ," + DelChiShuiEntitys[i];
                }
                Conditin += ")";
                model.delete("ChiShuiEntity", Conditin);
            }

            if (DelSpeedEntitys.Count > 0)
            {
                string Conditin = "ID in (" + DelSpeedEntitys[0];
                for (int i = 1; i < DelSpeedEntitys.Count; i++)
                {
                    Conditin += " ," + DelSpeedEntitys[i];
                }
                Conditin += ")";
                model.delete("SpeedEntity", Conditin);
            }

            ExperimentCombinationEntity entity = null;
            foreach (var c in ChiShuiEntitys)
            {
                foreach (var s in SpeedEntitys)
                {
                    if (c.ID > 0 && s.ID > 0)
                    {
                        entity = model.Query<ExperimentCombinationEntity>
                            ("ProjectID=" + _ProjectID + " and ExperimentType='" + ExperimentType.自航实验 + "' and DataCombination='" +c.Text+s.Text+"'").FirstOrDefault();
                    }
                    if (entity == null)
                        entity = new ExperimentCombinationEntity() 
                        {
                            CreateDate = DateTime.Now, 
                            ExperimentType = ExperimentType.自航实验.ToString(), 
                            DataCombination = c.Text + s.Text, 
                            IsFinished = "否", 
                            IsIng = "否", 
                            ProjectID = _ProjectID 
                        };
                    result.Add(entity);
                }
            }


            foreach (var c in ChiShuiEntitys)
            {
                if (c.ID > 0)
                    model.Update<ChiShuiEntity>(c);
                else
                    model.Save<ChiShuiEntity>(c);
            }
            foreach (var c in SpeedEntitys)
            {
                if (c.ID > 0)
                    model.Update<SpeedEntity>(c);
                else
                    model.Save<SpeedEntity>(c);
            }

           

            this.DialogResult = DialogResult.OK;

        }

        
        private void btnDraft_Click(object sender, EventArgs e)
        {

            DraftForm draftDlg = new DraftForm(0, _ProjectID, DataEntity.ExperimentType.自航实验);
            DialogResult rsltDraft = draftDlg.ShowDialog(this);
            if (rsltDraft == DialogResult.OK)
            {
                ChiShuiEntitys.Add(draftDlg.Entity);           
            }
        }

        private void btnSpeed_Click(object sender, EventArgs e)
        {
            SpeedForm speedDlg = new SpeedForm(0, _ProjectID, ExperimentType.自航实验);
            DialogResult rsltSpeed = speedDlg.ShowDialog(this);
            if (rsltSpeed == DialogResult.OK)
            {
                SpeedEntitys.Add(speedDlg.Entity);
            }
        }

        private void btnDraftDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewDraft.SelectedItems)
            {
                ChiShuiEntity entity = item.Tag as ChiShuiEntity;
                if (entity != null)
                {
                    ChiShuiEntitys.Remove(entity);
                    if (entity.ID > 0)
                    {
                        DelChiShuiEntitys.Add(entity.ID);
                    }
                }
                //listViewDraft.Items.Remove(item);

            }
        }

        private void btnSpeedDel_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in listViewSpeed.SelectedItems)
            {
                //listViewSpeed.Items.Remove(item);
                SpeedEntity entity = item.Tag as SpeedEntity;
                if (entity != null)
                {
                    SpeedEntitys.Remove(entity);
                    if (entity.ID > 0)
                    {
                        DelSpeedEntitys.Add(entity.ID);
                    }
                }
            }

        }

        private void Zihang_Load(object sender, EventArgs e)
        {
        }
    }
}
